function EnterPet(parent)
    local scene = LoadJson("pet.json");
    scene:setPosition(cc.p(0,0))
    parent:addChild(scene);
    
    scene.ui = scene.layer:getChildByName("Panel_1");
    LoadAllNode(scene, scene.ui);

    local petIdShift = 0;
    local petIdx = 1;
    local idxNum = 0;
    while scene["pet"..(idxNum+1)] do
        idxNum = idxNum + 1;
    end
    
    if EXCEL.pet[g_Player.petOn] then
        petIdx = g_Player.petOn;
        if petIdx>idxNum then
            petIdShift = petIdx-idxNum;
            petIdx = idxNum;
        end
    else
        for k,v in ipairs(EXCEL.pet) do
            local found = true;
            for k1,v1 in pairs(v.item) do
                if (g_Player.items[v1[1]] or 0) < v1[2] then
                    found = false;
                    break;
                end
            end

            if found then
                petIdx = k;
                if petIdx>idxNum then
                    petIdShift = petIdx-idxNum;
                    petIdx = idxNum;
                end
                break;
            end
        end
    end
    
    local function updateStatus()
        local petId = petIdx + petIdShift;
        
        local pet = EXCEL.pet[petId];
        local petLevel = g_Player.pets[petId];
        
        scene.activateText:setVisible(petLevel <= 0);
        scene.onText:setVisible(petLevel>0 and petId ~= g_Player.petOn);
        scene.offText:setVisible(g_Player.pets[petId]>0 and petId == g_Player.petOn);
        scene.upgradeText:setVisible(petLevel > 0);
        scene.upgrade:setVisible(petLevel>0)
        
        for i=1,idxNum do
            local petTmp = EXCEL.pet[i+petIdShift];
            scene["pet"..i]:loadTextures(petTmp.pngPath[1],petTmp.pngPath[2])
            scene["on"..i]:setVisible(g_Player.petOn==i+petIdShift);
            scene["pet"..i]:setBrightStyle(petId==i+petIdShift
                and ccui.BrightStyle.highlight or ccui.BrightStyle.normal);
        end
        
        local color = scene.animation:getColor();
        local node = scene.animation:getChildByName("uiAnimation")
        if node then
            node:removeFromParent()
        end
        
        node = LoadArmature(pet.uiAnimation, scene.animation, "uiAnimation");
        local size = scene.animation:getContentSize();
        node:setPosition(cc.p(size.width/2, size.height/2))
        
        scene.attr:setString(BuildAttr(pet.petAttr))
        
        scene.activateText:setVisible(g_Player.pets[petId]<=0);
        scene.onText:setVisible(g_Player.pets[petId]>0 and g_Player.petOn~=petId);
        scene.offText:setVisible(g_Player.pets[petId]>0 and g_Player.petOn==petId);
        scene.upgradeText:setVisible(g_Player.pets[petId]>0);
        
        local idx = 1;
        while true do
            local n = scene["num"..idx];
            if not n then
                break;
            end
            n:setVisible(false);
            idx = idx + 1;
        end
        for k,v in pairs(pet.item) do
            local n = scene["item"..k];
            if n then
                local item = EXCEL.item[v[1]];
                n:loadTexture(item.pathUI);
                
                local n = scene["num"..k];
                n:setVisible(true);
                local currentN = g_Player.items[v[1]]
                if currentN >= v[2] then
                    n:setColor(cc.c3b(0,255,0))
                else
                    n:setColor(cc.c3b(255,0,0))
                end
                n:setString(string.format("%s/%s", currentN, v[2]));
            end
        end
    end
    
    updateStatus();
    
    local function petCallBack(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            for i=1,idxNum do
                if sender:getName() == "pet"..i then
                    petIdx = i;
                    updateStatus();
                    break;
                end
            end
        end
    end

    for i=1,idxNum do
        local name = "pet"..i;
        if scene[name] then
            scene[name]:addTouchEventListener(petCallBack);
        end
    end
    
    local function activateCallBack(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            local petId = petIdx + petIdShift;
            
            local pet = EXCEL.pet[petId];
            local petLevel = g_Player.pets[petId];
            
            if petLevel <= 0 then
                if ReduceItemsData(pet.item) then
                    SetConfigData("pets", petId, 1);
                end
            else
                if petId == g_Player.petOn then
                    SetConfigData("petOn", nil, 0);
                else
                    SetConfigData("petOn", nil, petId);
                end
            end
            
            updateStatus();
        end
    end
    SetButtonFunc(scene.activate, activateCallBack, "activate")
    
    local function upgradeCallBack(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            local petId = petIdx + petIdShift;
            
            local pet = EXCEL.pet[petId];
            local petLevel = g_Player.pets[petId];
            
            if petLevel > 0 then
            
            end
            
            updateStatus();
        end  
    end
    scene.upgrade:addTouchEventListener(upgradeCallBack)
    
    local function prevCallBack(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if petIdShift > 0 then
                petIdShift = petIdShift - 1;
            end
            
            updateStatus();
        end  
    end
    scene.prev:addTouchEventListener(prevCallBack)
    
    local function nextCallBack(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if idxNum + petIdShift < #EXCEL.pet then
                petIdShift = petIdShift + 1;
            end
            
            updateStatus();
        end
    end
    scene.next:addTouchEventListener(nextCallBack)
    
    return scene;
end


